//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tools\fn_buildingPosCalculator.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

// THIS SHOULD NOT BE USED OUTSIDE OF THE BUILDING POS FUNCTIONS

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_building_pos","_building_dir","_pos_details","_relative_pos","_pos_planar","_dist","_pos_vertical","_dist_z","_x","_y","_z","_angle_xy","_angle_yx","_angle_xz","_initial_yx","_nx","_ny","_nz","_bx","_by","_bz","_real_pos","_real_dir","_stance"];
_building_pos = _this select 0;
_building_dir = _this select 1;
_pos_details = _this select 2;

_relative_pos = _pos_details select 0;
_real_dir = _pos_details select 1;
_stance = switch(toUpper(_pos_details select 2)) do {
	case "U": {"UP"};
	case "M": {"MIDDLE"};
	case "D": {"DOWN"};
	default { _pos_details select 2 };
};

_pos_planar = +_relative_pos;
_pos_planar set [2,0];
_dist = ([0,0,0] vectorDistance _pos_planar);

_pos_vertical = +_relative_pos;
_dist_z = ([0,0,0] vectorDistance _pos_vertical);

_x = _relative_pos select 0;
_y = _relative_pos select 1;
_z = _relative_pos select 2;
If (_dist == 0) then {
	_angle_xy = 0;
	_angle_yx = 0;
} else {
	_int = (_x / _dist);
	if (_int > 1) then {_int = 1};
	_angle_xy = asin _int;
	_int = (_y / _dist);
	if (_int > 1) then {_int = 1};
	_angle_yx = acos (_int);
};
if (_dist_z == 0) then {
	_angle_xz = 0;
} else {
	_int = (_z / _dist_z);
	if (_int > 1) then {_int = 1};
	_angle_xz = asin (_int);
};
_initial_yx = _angle_yx;
if (_angle_yx < 0) then {
	_angle_yx = 360 - _angle_yx;
};
if (_angle_yx > 360) then {
	_angle_yx = _angle_yx - 360;
};
If (((_x > 0) And (_y >=0)) And (_angle_yx >= 270)) then { _angle_yx = 360 - _angle_yx;};
If (((_x > 0) And (_y < 0)) And (_angle_yx >= 180)) then { _angle_yx = 360 - _angle_yx;};
If (((_x < 0) And (_y >=0)) And (_angle_yx <=  90)) then { _angle_yx = 360 - _angle_yx;};
If (((_x < 0) And (_y < 0)) And (_angle_yx <= 180)) then { _angle_yx = 360 - _angle_yx;};

// _angle_xy = _angle_xy + _building_dir;
_angle_yx = _angle_yx + _building_dir;
_real_dir = _real_dir + _building_dir;
//diag_log Format ["x:%1, y:%2, z:%3, iyx:%4, ayx:%5, axz:%6, bd:%7, dxy:%8, dxz:%9",_x,_y,_z,_initial_yx,_angle_yx,_angle_xz,_building_dir,_dist,_dist_z];

_nx = _dist * (sin _angle_yx);
_ny = _dist * (cos _angle_yx);
_nz = _dist_z * (sin _angle_xz);

_bx = _building_pos select 0;
_by = _building_pos select 1;
_bz = _building_pos select 2;

//diag_log Format ["nx:%1, ny:%2, nz:%3, bx:%4, by:%5, bz:%6",_nx,_ny,_nz,_bx,_by,_bz];
_nx = _bx + _nx;
_ny = _by + _ny;
_nz = _bz + _nz;
TRACE_1("Before height correction",ARR_5(_building_pos, _nx, _ny, _nz, (getTerrainHeightASL ARR_2(_nx,_ny)) - (getTerrainHeightASL ARR_2(_bx,_by))));
// If the terrain under the building in not level, we need to correct the position...
_nz = _nz + ((getTerrainHeightASL [_nx,_ny]) - (getTerrainHeightASL [_bx,_by]));

_real_pos = [[_nx, _ny, _nz],_real_dir,_stance];

TRACE_1("End",_real_pos);
TMFPROFILERSTOP;
_real_pos;